<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb>
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片区 -->
    <el-card>
      <el-row>
        <el-col :span="8">
          <el-input class="input-with-select" v-model="queryInfo.query">
            <el-button
              @click="queryOrder"
              slot="append"
              icon="el-icon-search"
            ></el-button>
          </el-input>
        </el-col>
      </el-row>
      <!-- 订单数据展示区域 -->
      <el-table :data="ordersList" border stripe style="width: 100%">
        <el-table-column type="index" width="70"></el-table-column>
        <el-table-column width="220" label="订单编号" prop="order_number">
        </el-table-column>
        <el-table-column width="90" label="订单价格" prop="order_price">
        </el-table-column>
        <el-table-column width="90" label="是否付款" prop="order_pay">
          <template slot-scope="scope">
            <el-tag type="success" v-if="scope.row.order_pay === '1'"
              >已付款</el-tag
            >
            <el-tag type="danger" v-else>未付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column width="90" label="是否发货" prop="is_send">
        </el-table-column>
        <el-table-column width="180" label="下单时间" prop="create_time">
          <template slot-scope="scope">
            {{ scope.row.create_time | dataFormat }}
          </template>
        </el-table-column>

        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              type="primary"
              size="mini"
              icon="el-icon-edit"
              @click="showCityDialog"
            ></el-button>
            <el-button
              type="info"
              icon="el-icon-setting"
              size="mini"
              @click="showKuaidiDialog"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 分页功能 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="this.queryInfo.pagenum"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="this.queryInfo.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="this.total"
    >
    </el-pagination>
    <!-- 省级联动对话框 -->
    <el-dialog
      title="修改地址"
      :visible.sync="setCityDialogVisible"
      width="50%"
    >
      <el-form :model="cityForm" ref="cityForm" label-width="100px">
        <el-form-item label="地址">
          <el-cascader
            v-model="cityForm.addr1"
            :options="cityData"
          ></el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogVisible = false"
          >确 定</el-button
        >
      </span>
    </el-dialog>

    <!-- 物流进度对话框 -->
    <el-dialog
      title="物流进度"
      :visible.sync="setWuLiuDialogVisible"
      width="50%"
    >
      <el-timeline reverse>
        <el-timeline-item
          v-for="(item, i) in progressInfo"
          :key="i"
          :timestamp="progressInfo.time"
        >
          {{ item.context }}
        </el-timeline-item>
      </el-timeline>
    </el-dialog>
  </div>
</template>
<script>
import cityData from './citydata'
export default {
  created() {
    this.getOrdersList()
  },
  data() {
    return {
      //订单数据列表
      ordersList: [],
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 5,
      },
      total: 0,
      //省级联动数据
      cityData,
      setCityDialogVisible: false,
      cityForm: {
        addr1: [],
        addr2: '',
      },
      setWuLiuDialogVisible: false,
      //物流进度信息
      progressInfo: [],
    }
  },

  methods: {
    async getOrdersList() {
      const { data: res } = await this.$http.get('orders', {
        params: this.queryInfo,
      })

      if (res.meta.status !== 200) {
        return this.$message.error('获取订单列表失败')
      }

      this.total = res.data.total
      this.ordersList = res.data.goods
      console.log(this.total)
      console.log(this.ordersList)
    },
    //每页条数发生变化时触发
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize
      this.getOrdersList()
    },
    //当前页发生变化时触发
    handleCurrentChange(nowPageNum) {
      this.queryInfo.pagenum = nowPageNum
      this.getOrdersList()
    },
    //查询指定query订单
    queryOrder() {
      this.getOrdersList()
      console.log(this.queryInfo.query)
    },
    //显示省级联动对话框
    showCityDialog() {
      this.setCityDialogVisible = true
    },
    //显示物流进度
    showKuaidiDialog() {
      this.getProgress()
      this.setWuLiuDialogVisible = true
    },
    //获取物流进度信息
    async getProgress() {
      //804909574412544580
      //1106975712662
      const testNum = '1106975712662'
      const { data: res } = await this.$http.get(`/kuaidi/${testNum}`)

      if (res.meta.status !== 200) {
        return this.$$message.error('获取物流信息失败')
      }

      this.progressInfo = res.data
    },
  },
}
</script>

<style lang="less" scoped>
.el-icon-check {
  color: #fff;
  border-radius: 50% 50%;
  background-color: green;
}
</style>